﻿#pragma once
#include "XyNet/Global.h"

namespace XyNet
{
	template <class Target, class Source>
	inline Target polymorphic_downcast(Source* x)
	{
		assert(dynamic_cast<Target>(x) == x);
		return static_cast<Target>(x);
	}

	template <class Target, class Source>
	inline Target polymorphic_downcast(Source& x)
	{
#ifdef _DEBUG
		return dynamic_cast<Target>(x);
#else
		return static_cast<Target>(x);
#endif
	}

	template<class T, class U>
	inline shared_ptr<T> polymorphic_pointer_downcast(const shared_ptr<U>& r)
	{
		assert(dynamic_cast<T*>(r.get()) == r.get());
		return static_pointer_cast<T>(r);
	}

#ifdef _DEBUG
#	define XYNET_DOWNCAST XyNet::polymorphic_downcast
#	define XYNET_POINTER_DOWNCAST XyNet::polymorphic_pointer_downcast
#else
#	define XYNET_DOWNCAST static_cast
#	define XYNET_POINTER_DOWNCAST static_pointer_cast
#endif
}